package com.haochang.chunkvod.view.vod.dialog;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 *     @author Yangjie
 *     className VodSimpleDialog
 *     created at  2016/11/9  10:37
 */

public class VodCustomDialog extends Dialog
{
	public VodCustomDialog(Context context)
	{
		super(context);
	}

	public VodCustomDialog(Context context, int theme)
	{
		super(context, theme);
	}

	protected VodCustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener)
	{
		super(context, cancelable, cancelListener);
	}

	public static class Builder
	{
		private Context context;
		private CharSequence title; //对话框标题

		public Builder(Context context)
		{
			this.context = context;
		}

		public Builder setTitle(int title)
		{
			this.title = context.getText(title);
			return this;
		}

		public Builder setTitle(CharSequence title)
		{
			this.title = title;
			return this;
		}

		/**
		 *
		 * @param layoutResourceId 传入的布局文件
		 * @param contentHeightPx  内容区域的高度(单位px)   
		 * @param listener 传入布局文件后，里面的内容自己处理      
		 * @return VodCustomDialog
		 */
		public VodCustomDialog showSimpleDialog(int layoutResourceId, int contentHeightPx, OnViewAddedListener listener)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final VodCustomDialog dialog = new VodCustomDialog(context, R.style.Alert_Dialog);
			final ViewGroup viewGroup = null;
			LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.common_dialog_custom_toast, viewGroup, false);
			View view = inflater.inflate(layoutResourceId, layout, false);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT,  LayoutParams.WRAP_CONTENT));
			if (view != null)
			{
				if (contentHeightPx > 0)
				{
					LinearLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
					layoutParams.height = contentHeightPx;
					view.setLayoutParams(layoutParams);
				}
				layout.addView(view);
				if (null != listener)
					listener.getView(view);
			}
			BaseTextView tv_title = (BaseTextView) layout.findViewById(R.id.tv_title);
			tv_title.setText(title);
			layout.findViewById(R.id.iv_cancel).setOnClickListener(new OnBaseClickListener()
			{
				@Override
				public void onBaseClick(View v)
				{
					dialog.dismiss();
				}
			});
			return dialog;
		}

		public void showDialog(int layoutResourceId, OnViewAddedListener listener)
		{
			showDialog(layoutResourceId, 0, 20, listener);
		}
		public void showDialog(int layoutResourceId, int contentHeightPx, OnViewAddedListener listener)
		{
			showDialog(layoutResourceId, contentHeightPx, 20, listener);
		}
		/**
		 * 显示window全宽，marginLeft，marginRight 20dp
		 * @param layoutResourceId 传入的布局文件
		 * @param contentHeightPx  内容区域的高度(单位px)   
		 * @param listener 传入布局文件后，里面的内容自己处理   
		 * @param marginLeftAndmarginRight 左边距和右边距的值dp
		 * 如果小于0或者大于200则置为20
		 */
		public void showDialog(int layoutResourceId, int contentHeightPx, int marginLeftAndmarginRight, OnViewAddedListener listener)
		{
			VodCustomDialog dialog = showSimpleDialog(layoutResourceId, contentHeightPx, listener);
			dialog.show();
			Window window = dialog.getWindow();
			if (window != null)
			{
				WindowManager.LayoutParams layoutParams = window.getAttributes();
				layoutParams.width = ScreenUtils.getScreenWidth(context)
						- 2 * DipPxConversion.dip2px(context, marginLeftAndmarginRight >= 200 || marginLeftAndmarginRight < 0 ? 20 : marginLeftAndmarginRight);
				window.setAttributes(layoutParams);
				window.setGravity(Gravity.CENTER);
				window.setBackgroundDrawable(new ColorDrawable(0));
				dialog.setCancelable(true);
				dialog.setCanceledOnTouchOutside(true);
			}
		}
	}

	/**
	 * addview后的监听接口
	 */
	public interface OnViewAddedListener
	{
		void getView(View view);
	}
}
